<?php
/**
 * 系统员工
 */
namespace App\Http\Controllers;

use App\Models\Manage;
use App\Util\Qrcode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ManageController extends Controller
{
    /**
     * 员工列表
     */
    public function index()
    {
        $Manage = Manage::where('identity','check')->get();
        if($Manage) {
            foreach($Manage as $item) {
                $item->member;
            }
        }
        return response(['data'=>$Manage]);
    }

    // 申请员工二维码
    public function qrcode(Request $request)
    {
        $Qrcode = new Qrcode();
        $_params = $Qrcode->createParams($request->data);
        return response(['data'=>route('scan',$_params)]);
    }

    /**
     * 检测二维码是否过期||申请用户是否已经扫码
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function testQrcode(Request $request)
    {
        // 先检测用户是否已经扫码
        $_scan_manage_member = Cache::get('scan_manage_member');
        $_result = array("data"=>1);
        if($_scan_manage_member) {
            $_result['member'] = $_scan_manage_member;
            Cache::forget('scan_manage_member');
            $_response = response($_result);
        } else {
            $Qrcode = new Qrcode();
            if(!$Qrcode->testQrcode($request->data)) {
                $_result = array("data"=>0);
            }
            $_response = response($_result);
        }
        return $_response;
    }

    /**
     * 添加员工
     */
    public function store(Request $request)
    {
        $_data = Validator::make(
            $request->all(),
            [
                'member_id' => [
                    'required',
                    'exists:members,id'
                ],
                'email' => [
                    'required',
                    'email',
                    'unique:manages,email'
                ]
            ],
            [
                'member_id.required' => 'member_id不能为空',
                'member_id.exists' => '用户不存在',
                'email.required' => '邮箱不能为空',
                'email.email' => '请设置正确的邮箱',
                'email.unique' => '邮箱已存在'
            ]
        )->validate();
        try{
            if(Manage::where('member_id',$_data['member_id'])->where("identity",'check')->exists()) {
                throw new \Exception('当前用户已经是员工了',402);
            }
            $_data['identity'] = 'check';
            if(Manage::create($_data)) {
                return response(['data'=>'success']);
            } else {
                throw new \Exception('添加失败',402);
            }
        }catch (\Exception $e) {
            return response(['message'=>$e->getMessage()],$e->getCode());
        }
    }

    /**
     * 修改员工
     */
    public function edit($_id,Request $request)
    {
        $_data = Validator::make(
            $request->all(),
            [
                'member_id' => [
                    'required',
                    'exists:members,id'
                ],
                'email' => [
                    'required',
                    'email',
                    Rule::unique('manages','email')->ignore($_id)
                ]
            ],
            [
                'member_id.required' => 'member_id不能为空',
                'member_id.exists' => '用户不存在',
                'email.required' => '邮箱不能为空',
                'email.email' => '请设置正确的邮箱',
                'email.unique' => '邮箱已存在'
            ]
        )->validate();
        try{
            if(Manage::where("member_id",$_data['member_id'])
                ->where('identity','check')->where('id','!=',$_id)->exists()) {
                throw new \Exception('当前用户已经是员工了',402);
            }
            if(Manage::where('id',$_id)->update($_data)) {
                return response(['data'=>'success']);
            } else {
                return new \Exception('修改失败', 402);
            }
        }catch (\Exception $e) {
            return response(['message'=>$e->getMessage()],$e->getCode());
        }
    }

    /**
     * 删除员工
     */
    public function destroy($_id)
    {
        if(Manage::destroy($_id)) {
            return response(['data'=>'success']);
        } else {
            return response(['message'=>'操作失败']);
        }
    }
}
